package com.varton.v3;

import java.io.*;

public class MyClassLoader extends ClassLoader{

    public Class<?> findClass(String name) throws ClassNotFoundException {
        //1.读到内存
        String prefix = ProxyProducer.class.getResource("").getPath();
        File base = new File(prefix);
        File f = new File(base,"$Proxy.class");
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            byte[] buff = new byte[1024];
            in = new FileInputStream(f);
            out = new ByteArrayOutputStream();
            int len;
            while ((len = in.read(buff)) > -1){
                out.write(buff,0,len);
            }
            return this.defineClass(name,out.toByteArray(),0,out.size());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            f.delete();//删除.class文件
        }
        //2.写入jvm
        return null;
    }
}
